/*
 * GraphConnectionInfoPanel.java
 *
 * Created on June 29, 2008, 5:10 PM
 */
package pl.edu.agh.ssm.monitor.gui;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.LinkedList;
import java.util.List;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;
import pl.edu.agh.ssm.monitor.SessionMonitorApp;
import pl.edu.agh.ssm.monitor.data.RTSPPacket;
import pl.edu.agh.ssm.monitor.data.SessionConnection;
import pl.edu.agh.ssm.monitor.data.SessionMediaType;
import pl.edu.agh.ssm.monitor.utils.IconUtil;

/**
 *
 * @author  aneezka
 */
@SuppressWarnings("serial")
public class GraphConnectionInfoPanel extends javax.swing.JPanel implements UpdatePanel, IconPanel {

    /** Creates new form GraphConnectionInfoPanel */
    public GraphConnectionInfoPanel(SessionConnection connection) {
        this.connection = connection;
        initComponents();
        JPanel panel = new ConnectionGeneralInfoPanel(connection);
        final JScrollPane pane = new JScrollPane(panel);
        pane.setBorder(new TitledBorder(panel.getName()));
        jPanel2.add(pane);
        panels.add((UpdatePanel) panel);
        /**test**/
//        RTSPPacket packet;
//        RTSPAnswer answer;
//        for (RTSPPacketType t : RTSPPacketType.values()) {
//            packet = new RTSPPacket();
//            packet.setCSeq(37384L);
//            packet.setType(t);
//            packet.setFullPacket("dfka;ljf;lasfjas\ndfkasdfj;aksjf;das\nfjdsak;jf;skajf\ndkdkdkdkdkd");
//            packet.setTypeAttr("dkdkdkdkd");
//            packet.setPacketContent("fkldshgjklsdhgjlksdhjglksdfjklgnsdjklngjkrlsdngjklrsndjklgnsdjkljkgsdrlnbjgkrsjgj");
//            answer = new RTSPAnswer();
//            answer.setAnswerCode(220);
//            packet.setAnswer(answer);
//            connection.addRtspPacket(packet);
//        }
//        connection.getMediaDesc().setMediaType(SessionMediaType.RTSP_CONTROL);
        if (connection.getMediaDesc().getMediaType().equals(SessionMediaType.RTSP_CONTROL)) {
            rtspPanel = new RTSPTablePanel(connection);
            panel = rtspPanel;
            jButton2.setEnabled(true);
            jButton3.setEnabled(true);
            final JScrollPane pane2 = new JScrollPane(panel);
            pane2.setBorder(new TitledBorder(panel.getName()));
            jPanel2.add(pane2);
            panels.add((UpdatePanel) panel);
            rtspPanel.addListener(new UpdatePanelListener() {

                public void panelUpdated(UpdatePanel panel) {
                }

                public void panelRemoved(UpdatePanel panel) {
                    jPanel2.remove(pane2);
                    jPanel2.revalidate();
                    panels.remove(panel);
                }
            });
        } else if (connection.getReceiverReport() != null) {
            addRRPanel(new ReceiverReportPanel(connection));
        }
    }

    public void update() {
        jLabel1.setText("Connection " + connection.getConnectionAddress().getHostAddress() + ":" + connection.getConnectionPort());
        if (connection.getReceiverReport() != null && rrPanel == null) {
            addRRPanel(new ReceiverReportPanel(connection));
        }
        for (UpdatePanel p : panels) {
            p.update();
        }
        for (UpdatePanelListener l : listeners) {
            l.panelUpdated(this);
        }
    }

    public void remove() {
        for (UpdatePanelListener l : listeners) {
            l.panelRemoved(this);
        }
    }

    public void addListener(UpdatePanelListener listener) {
        listeners.add(listener);
    }

    public void removeListener(UpdatePanelListener listener) {
        listeners.remove(listener);
    }

    public Icon getIcon() {
        return (new IconUtil()).getConnectionIcon(connection);
    }

    private void addRRPanel(ReceiverReportPanel panel) {
        rrPanel = panel;
        final JScrollPane pane2 = new JScrollPane(panel);
        pane2.setBorder(new TitledBorder(panel.getName()));
        jPanel2.add(pane2);
        panels.add((UpdatePanel) panel);
        ((UpdatePanel) panel).addListener(new UpdatePanelListener() {

            public void panelUpdated(UpdatePanel panel) {
            }

            public void panelRemoved(UpdatePanel panel) {
                jPanel2.remove(pane2);
                jPanel2.revalidate();
                panels.remove(panel);
                rrPanel = null;
            }
            });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();

        setName("Form"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(pl.edu.agh.ssm.monitor.SessionMonitorApp.class).getContext().getResourceMap(GraphConnectionInfoPanel.class);
        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText("Connection " + connection.getConnectionAddress().getHostAddress() + ":" + connection.getConnectionPort());
        jLabel1.setName("jLabel1"); // NOI18N

        jButton1.setIcon(resourceMap.getIcon("jButton1.icon")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setToolTipText(resourceMap.getString("jButton1.toolTipText")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setIcon(resourceMap.getIcon("jButton2.icon")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setToolTipText(resourceMap.getString("jButton2.toolTipText")); // NOI18N
        jButton2.setEnabled(false);
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setIcon(resourceMap.getIcon("jButton3.icon")); // NOI18N
        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setToolTipText(resourceMap.getString("jButton3.toolTipText")); // NOI18N
        jButton3.setEnabled(false);
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 213, Short.MAX_VALUE)
                .addComponent(jButton3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(jButton2)
                    .addComponent(jButton1)
                    .addComponent(jButton3))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 467, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 244, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        SessionMonitorApp.getApplication().getView().updateBottomTab(this);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                JFrame frame = new JFrame();
                RTSPTablePanel panel = new RTSPTablePanel(rtspPanel.getConnection());
                frame.getContentPane().add(panel);
                frame.setLocationRelativeTo(SessionMonitorApp.getApplication().getView().getFrame());
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                frame.setTitle("RTSP Packets");
                frame.addWindowListener(new WindowAdapter() {

                    @Override
                    public void windowActivated(WindowEvent e) {
                        super.windowActivated(e);
                        rtspPanel.update();
                    }
                });
                frame.setSize(600, 400);
                frame.setVisible(true);
            }
        });
        
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        if (rtspPanel.getSelectedPacket() != null) {
            SwingUtilities.invokeLater(new Runnable() {

                public void run() {
                    JFrame frame = new JFrame();
                    JTextArea textArea = new JTextArea();
                    RTSPPacket packet = rtspPanel.getSelectedPacket();
                    textArea.setText("RTSP Packet:\n" + packet.getFullPacket() + "\n" + "RTSP Answer:\n" + (packet.getAnswer() == null ? "" : packet.getAnswer().getFullPacket()));
                    frame.getContentPane().add(new JScrollPane(textArea));
                    frame.setLocationRelativeTo(SessionMonitorApp.getApplication().getView().getFrame());
                    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    frame.setTitle("RTSP Packet");
                    frame.setSize(400, 400);
                    frame.setVisible(true);
                }
            });
        }
    }//GEN-LAST:event_jButton3ActionPerformed
    private SessionConnection connection;
    private List<UpdatePanel> panels = new LinkedList<UpdatePanel>();
    private List<UpdatePanelListener> listeners = new LinkedList<UpdatePanelListener>();
    private RTSPTablePanel rtspPanel;
    private ReceiverReportPanel rrPanel = null;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration//GEN-END:variables
}
